package com.huangwenshuo.futurestarschoolmanagement.controller;

import com.alibaba.fastjson.JSONObject;
import com.huangwenshuo.futurestarschoolmanagement.exception.CustomRuntimeException;
import com.huangwenshuo.futurestarschoolmanagement.mapper.UsersMapper;
import com.huangwenshuo.futurestarschoolmanagement.pojo.dto.UserDto;
import com.huangwenshuo.futurestarschoolmanagement.pojo.enums.RequestStateEnum;
import com.huangwenshuo.futurestarschoolmanagement.pojo.vo.Result;
import com.huangwenshuo.futurestarschoolmanagement.pojo.vo.UserInfoVo;
import com.huangwenshuo.futurestarschoolmanagement.service.UsersService;
import com.huangwenshuo.futurestarschoolmanagement.utils.ResUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/auth")
public class AuthController {


    @Autowired
    private UsersService userService;

    @Autowired
    @Lazy
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    /**
     * 注册
     *
     * @param registerUser
     * @return
     */
    @PostMapping("/register")
    public Result registerUser(@RequestBody UserDto.RegisterUser registerUser) {
        try {
            userService.register(registerUser);
        } catch (CustomRuntimeException e) {
            return ResUtils.result(RequestStateEnum.FAILED.value(), "注册失败, " + e.getMessage());
        }
        return ResUtils.result(RequestStateEnum.SUCCESS.value(), "注册成功");
    }

    /**
     * 获取用户信息
     *
     * @param request
     * @return
     */
    @GetMapping("/info")
    public Result userInfo(HttpServletRequest request) {
        Integer uid = (Integer) request.getAttribute("uid");
        UserInfoVo userInfoVo = null;
        try {
            userInfoVo = userService.getUserInfo(uid);
        } catch (CustomRuntimeException e) {
            return ResUtils.result(RequestStateEnum.FAILED.value(), "获取失败, " + e.getMessage());
        }
        return ResUtils.result(RequestStateEnum.SUCCESS.value(),userInfoVo,"获取成功");
    }

    /**
     * 多用户导入
     * @return
     */
    @PostMapping("uploadRegister")
    public Result uploadRegister(@RequestParam("users") MultipartFile uploadFile){
            return userService.uploadRegister(uploadFile);
    }

    @PostMapping("registerMore")
    public Result registerMore(@RequestBody List<UserDto.RegisterUser> registerUser){
        return userService.registerMore(registerUser);
    }
    @PostMapping("updateUserInfo")
    public Result updateUserInfo(@RequestBody UserDto.updateUserInfo updateUserInfo){
        return userService.updateUserInfo(updateUserInfo);
    }

    @PostMapping("updateUserPwd")
    public Result updateUserPwd(@RequestBody UserDto.updateUserPwd updateUserPwd){
        try {
            userService.updateUserPwd(updateUserPwd);
        } catch (CustomRuntimeException e) {
            return ResUtils.result(RequestStateEnum.FAILED.value(), "修改失败, " + e.getMessage());
        }
        return ResUtils.result(RequestStateEnum.SUCCESS.value(), "修改成功");
    }
    
}